package com.resource.map
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	public class MapTile extends Bitmap
	{
		private var _url:String;
		
		public function MapTile(bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			super(bitmapData, pixelSnapping, smoothing);
		}
		
		public function set url(value:String):void{
			if(_url != value){
				_url = value;
				var bmd:BitmapData = MapCacher.getInstance().getMapTile(_url);
				if(bmd){
					bitmapData = bmd;
				}else{
					MapCacher.getInstance().addEventListener(_url, onLoaded);
					MapCacher.getInstance().addEventListener(_url+"error", onError);
					MapCacher.getInstance().load(_url);
				}
			}
		}
		
		private function onLoaded(event:MapEvent):void{
			var loader:MapLoader = event.loader;
			MapCacher.getInstance().removeEventListener(loader.url, onLoaded);
			MapCacher.getInstance().removeEventListener(loader.url+"error", onError); 
			bitmapData = event.data;
		}
		
		private function onError(event:MapError):void{
			var loader:MapLoader = event.loader;
			MapCacher.getInstance().removeEventListener(loader.url, onLoaded);
			MapCacher.getInstance().removeEventListener(loader.url+"error", onError); 
		}
	}
}